<template>
  <div class="router-tutorial">
    <div class="tutorial-content">
      <h2>Vue Router 基础</h2>
      <p>Vue Router 是 Vue.js 官方的路由管理器。它与 Vue.js 核心深度集成，让构建单页面应用变得容易。</p>
      
      <h3>基本使用步骤：</h3>
      <ol>
        <li>安装 Vue Router</li>
        <li>创建路由组件</li>
        <li>配置路由</li>
        <li>创建路由实例</li>
        <li>在应用中使用路由</li>
      </ol>
      
      <h3>安装</h3>
      <p>在项目中已经安装好了 Vue Router</p>
      <pre>npm install vue-router@4</pre>
      
      <h3>创建路由组件</h3>
      <p>在右侧编辑器中，您可以看到我们已经创建了两个组件：Home.vue 和 About.vue</p>
      
      <h3>配置路由</h3>
      <p>在 router.js 文件中，我们定义了路由配置：</p>
      
      <h3>在应用中使用路由</h3>
      <p>在 App.vue 中，我们使用 &lt;router-view&gt; 来显示当前路由组件，使用 &lt;router-link&gt; 进行导航</p>
      
      <h3>试试看</h3>
      <p>点击右侧编辑器中的链接，观察路由变化</p>
    </div>
    
    <div class="code-editor">
      <div class="file-tabs">
        <button @click="currentFile = 'App'">App.vue</button>
        <button @click="currentFile = 'Home'">Home.vue</button>
        <button @click="currentFile = 'About'">About.vue</button>
        <button @click="currentFile = 'router'">router.js</button>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'App'">
        <pre><code>{{ appTemplate }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'Home'">
        <pre><code>{{ homeTemplate }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'About'">
        <pre><code>{{ aboutTemplate }}</code></pre>
      </div>
      
      <div class="code-preview" v-if="currentFile === 'router'">
        <pre><code>{{ routerTemplate }}</code></pre>
      </div>
      
      <div class="result-preview">
        <h4>预览效果：</h4>
        <div class="preview-container">
          <div class="app-container">
            <h1>Vue Router 示例</h1>
            <div class="nav-links">
              <a href="#" @click.prevent="activePage = 'home'">首页</a> | 
              <a href="#" @click.prevent="activePage = 'about'">关于</a>
            </div>
            <div class="route-view">
              <component :is="currentComponent" />
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue';
import Home from './components/Home.vue';
import About from './components/About.vue';

const currentFile = ref('App');
const activePage = ref('home');

const currentComponent = computed(() => {
  return activePage.value === 'home' ? Home : About;
});

// 预定义模板
const appTemplate = `<template>
  <div id="app">
    <h1>Vue Router 示例</h1>
    <div class="nav-links">
      <router-link to="/">首页</router-link> | 
      <router-link to="/about">关于</router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script setup>
// App.vue 不需要特殊逻辑
<\/script>

<style>
.nav-links {
  margin: 20px 0;
}
</style>`;

const homeTemplate = `<template>
  <div class="home">
    <h2>首页</h2>
    <p>这是Vue Router教程的首页组件</p>
  </div>
</template>

<script setup>
// 使用 <script setup> 语法
<\/script>

<style scoped>
.home {
  padding: 20px;
  background-color: #f5f5f5;
  border-radius: 5px;
}
</style>`;

const aboutTemplate = `<template>
  <div class="about">
    <h2>关于页面</h2>
    <p>这是Vue Router教程的关于页面组件</p>
  </div>
</template>

<script setup>
// 使用 <script setup> 语法
<\/script>

<style scoped>
.about {
  padding: 20px;
  background-color: #e8f4fc;
  border-radius: 5px;
}
</style>`;

const routerTemplate = `import { createRouter, createWebHistory } from 'vue-router'
import Home from './components/Home.vue'
import About from './components/About.vue'

const routes = [
  {
    path: '/',
    name: 'home',
    component: Home
  },
  {
    path: '/about',
    name: 'about',
    component: About
  }
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

export default router`;
</script>

<style scoped>
.router-tutorial {
  display: flex;
  height: 100%;
}

.tutorial-content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
  border-right: 1px solid #ddd;
}

.code-editor {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.file-tabs {
  display: flex;
  background-color: #f0f0f0;
  border-bottom: 1px solid #ddd;
}

.file-tabs button {
  padding: 8px 16px;
  border: none;
  background: none;
  cursor: pointer;
}

.file-tabs button:hover {
  background-color: #e0e0e0;
}

.code-preview {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  background-color: #f9f9f9;
}

pre {
  margin: 0;
  white-space: pre-wrap;
  font-family: monospace;
}

.result-preview {
  height: 300px;
  border-top: 1px solid #ddd;
  padding: 10px;
  overflow-y: auto;
}

.preview-container {
  border: 1px solid #ddd;
  border-radius: 5px;
  padding: 15px;
  background-color: white;
}

.app-container {
  text-align: center;
}

.nav-links {
  margin: 20px 0;
}

.route-view {
  margin-top: 20px;
  min-height: 100px;
}
</style> 